﻿
//***********************************************************************
// * Module:   TbBstBsznHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/5
// * Purpose: Definition of the Class TbBstBsznHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using System.Collections;
using JWBSTAPP.CommonClass;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstBsznHandler 的摘要说明
    /// </summary>
    public class TbBstBsznHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                var mOrg = AppOrganizatiOnBLL.Load(mUser.OrgnaId);
                TbBstBszn mTbBstBszn = new TbBstBszn();
                mTbBstBszn.Bsznid = Guid.NewGuid().ToString(); //主键
                mTbBstBszn.Swmc = context.Request.Form["texSwmc"]; //事物名称
                mTbBstBszn.Cjrq = DateTime.Now; //创建日期
                mTbBstBszn.Yhzh = mUser.UserLoginname; //用户帐号
                mTbBstBszn.Llr = mUser.UserName;//录入人
                mTbBstBszn.Shr = context.Request.Form["texShr"]; //审核人
                mTbBstBszn.Sfsh = context.Request.Form["texSfsh"]; //是否审核
                mTbBstBszn.Lmbs = context.Request.Form["texLmbs"]; //栏目标识
                mTbBstBszn.Ztflbm = context.Request.Form["texZtflbm"]; //主题分类编码
                mTbBstBszn.Xgsj = Convert.ToDateTime("0001-1-1"); //修改时间
                mTbBstBszn.Ztfl = context.Request.Form["texZtfl"]; //主题分类
                mTbBstBszn.Sfrd = context.Request.Form["selEnabled"];//是否热点 0非热点 1热点
                mTbBstBszn.Fwdxfl = context.Request.Form["texFwdxfl"]; //服务对象分类
                mTbBstBszn.Sxlb = context.Request.Form["texSxlb"]; //事项类别
                mTbBstBszn.Bsjg = context.Request.Form["texBsjg"]; //办事机构
                mTbBstBszn.Sbfw = context.Request.Form["texSbfw"]; //申报范围
                mTbBstBszn.Sbtj = context.Request.Form["texSbtj"]; //申报条件
                mTbBstBszn.Blyj = context.Request.Form["texBlyj"]; //办理依据
                mTbBstBszn.Blsxzj = context.Request.Form["texBlsxzj"]; //办理所需证件
                mTbBstBszn.Tjcl = context.Request.Form["texTjcl"]; //提交材料
                mTbBstBszn.Bllc = context.Request.Form["texBllc"]; //办理流程
                mTbBstBszn.Bgxz = context.Request.Form["texBgxz"]; //表格下载
                mTbBstBszn.Blqx = context.Request.Form["texBlqx"]; //办理期限
                mTbBstBszn.Gzsj = context.Request.Form["texGzsj"]; //工作时间
                mTbBstBszn.Bldd = context.Request.Form["texBldd"]; //办理地点
                mTbBstBszn.Zxdh = context.Request.Form["texZxdh"]; //咨询电话
                mTbBstBszn.Wssq = context.Request.Form["texWssq"]; //网上申请
                mTbBstBszn.Wszx = context.Request.Form["texWszx"]; //网上咨询
                mTbBstBszn.Wscx = context.Request.Form["texWscx"]; //网上查询
                mTbBstBszn.Sfbz = context.Request.Form["texSfbz"]; //收费标准
                mTbBstBszn.Cjwt = context.Request.Form["texCjwt"]; //常见问题
                mTbBstBszn.Gkfs = context.Request.Form["texGkfs"]; //公开方式
                mTbBstBszn.Bgkly = context.Request.Form["texBgkly"]; //不公开理由
                mTbBstBszn.Gksx = context.Request.Form["texGksx"]; //公开时限
                mTbBstBszn.Gkfw = context.Request.Form["texGkfw"]; //公开范围
                mTbBstBszn.Shcx = context.Request.Form["texShcx"]; //审核程序
                mTbBstBszn.Zrbm = context.Request.Form["texZrbm"]; //责任部门
                if (string.IsNullOrEmpty(context.Request.Form["texFwsj"]))
                {

                }
                else
                {
                    mTbBstBszn.Fwsj = Convert.ToDateTime(context.Request.Form["texFwsj"]); //发文时间
                }
                mTbBstBszn.Syh = context.Request.Form["texSyh"]; //索引号
                mTbBstBszn.Xxsxh = context.Request.Form["texXxsxh"]; //信息顺序号
                mTbBstBszn.Ztc = context.Request.Form["texZtc"]; //主题词
                mTbBstBszn.Tcfl = context.Request.Form["texTcfl"]; //体裁分类
                mTbBstBszn.Zy = context.Request.Form["texZy"]; //摘要
                if (string.IsNullOrEmpty(context.Request.Form["texKssj"]))
                {

                }
                else
                {
                    mTbBstBszn.Kssj = Convert.ToDateTime(context.Request.Form["texKssj"]); //开始时间
                }
                if (string.IsNullOrEmpty(context.Request.Form["texJssj"]))
                {

                }
                else
                {
                    mTbBstBszn.Jssj = Convert.ToDateTime(context.Request.Form["texJssj"]); //结束时间
                }
                mTbBstBszn.Zxljdz = context.Request.Form["texZxljdz"]; //在线链接地址
                mTbBstBszn.Cid = Convert.ToDecimal(context.Request.Form["selCid"]); //分类名称
                mTbBstBszn.Bszngxsj = DateTime.Now; //数据最后更新时间
                mTbBstBszn.Sfsc = "0"; // 0未删除1已删除 

                mResultModel = TbBstBsznBLL.Insert(mTbBstBszn, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstBszn mTbBstBszn = new TbBstBszn();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstBsznBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstBszn mTbBstBszn = new TbBstBszn();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstBszn = TbBstBsznBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstBszn != null)
                {
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    var mOrg = AppOrganizatiOnBLL.Load(mUser.OrgnaId);
                    mTbBstBszn.Swmc = context.Request.Form["texSwmc"]; //事物名称
                    //mTbBstBszn.Cjrq = Convert.ToDateTime(context.Request.Form["texCjrq"]); //创建日期
                    mTbBstBszn.Yhzh = mUser.UserLoginname; //用户帐号
                    mTbBstBszn.Llr = mUser.UserName; //录入人
                    mTbBstBszn.Shr = context.Request.Form["texShr"]; //审核人
                    mTbBstBszn.Sfsh = context.Request.Form["texSfsh"]; //是否审核
                    mTbBstBszn.Lmbs = context.Request.Form["texLmbs"]; //栏目标识
                    mTbBstBszn.Ztflbm = context.Request.Form["texZtflbm"]; //主题分类编码
                    mTbBstBszn.Xgsj = DateTime.Now; //修改时间
                    mTbBstBszn.Sfrd = context.Request.Form["selEnabled"];//是否热点 0非热点 1热点
                    mTbBstBszn.Ztfl = context.Request.Form["texZtfl"]; //主题分类
                    mTbBstBszn.Fwdxfl = context.Request.Form["texFwdxfl"]; //服务对象分类
                    mTbBstBszn.Sxlb = context.Request.Form["texSxlb"]; //事项类别
                    mTbBstBszn.Bsjg = context.Request.Form["texBsjg"]; //办事机构
                    mTbBstBszn.Sbfw = context.Request.Form["texSbfw"]; //申报范围
                    mTbBstBszn.Sbtj = context.Request.Form["texSbtj"]; //申报条件
                    mTbBstBszn.Blyj = context.Request.Form["texBlyj"]; //办理依据
                    mTbBstBszn.Blsxzj = context.Request.Form["texBlsxzj"]; //办理所需证件
                    mTbBstBszn.Tjcl = context.Request.Form["texTjcl"]; //提交材料
                    mTbBstBszn.Bllc = context.Request.Form["texBllc"]; //办理流程
                    mTbBstBszn.Bgxz = context.Request.Form["texBgxz"]; //表格下载
                    mTbBstBszn.Blqx = context.Request.Form["texBlqx"]; //办理期限
                    mTbBstBszn.Gzsj = context.Request.Form["texGzsj"]; //工作时间
                    mTbBstBszn.Bldd = context.Request.Form["texBldd"]; //办理地点
                    mTbBstBszn.Zxdh = context.Request.Form["texZxdh"]; //咨询电话
                    mTbBstBszn.Wssq = context.Request.Form["texWssq"]; //网上申请
                    mTbBstBszn.Wszx = context.Request.Form["texWszx"]; //网上咨询
                    mTbBstBszn.Wscx = context.Request.Form["texWscx"]; //网上查询
                    mTbBstBszn.Sfbz = context.Request.Form["texSfbz"]; //收费标准
                    mTbBstBszn.Cjwt = context.Request.Form["texCjwt"]; //常见问题
                    mTbBstBszn.Gkfs = context.Request.Form["texGkfs"]; //公开方式
                    mTbBstBszn.Bgkly = context.Request.Form["texBgkly"]; //不公开理由
                    mTbBstBszn.Gksx = context.Request.Form["texGksx"]; //公开时限
                    mTbBstBszn.Gkfw = context.Request.Form["texGkfw"]; //公开范围
                    mTbBstBszn.Shcx = context.Request.Form["texShcx"]; //审核程序
                    mTbBstBszn.Zrbm = context.Request.Form["texZrbm"]; //责任部门
                    mTbBstBszn.Fwsj = Convert.ToDateTime(context.Request.Form["texFwsj"]); //发文时间
                    mTbBstBszn.Syh = context.Request.Form["texSyh"]; //索引号
                    mTbBstBszn.Xxsxh = context.Request.Form["texXxsxh"]; //信息顺序号
                    mTbBstBszn.Ztc = context.Request.Form["texZtc"]; //主题词
                    mTbBstBszn.Tcfl = context.Request.Form["texTcfl"]; //体裁分类
                    mTbBstBszn.Zy = context.Request.Form["texZy"]; //摘要
                    mTbBstBszn.Kssj = Convert.ToDateTime(context.Request.Form["texKssj"]); //开始时间
                    mTbBstBszn.Jssj = Convert.ToDateTime(context.Request.Form["texJssj"]); //结束时间
                    mTbBstBszn.Zxljdz = context.Request.Form["texZxljdz"]; //在线链接地址
                    mTbBstBszn.Cid = Convert.ToDecimal(context.Request.Form["selCid"]); //分类名称
                    mTbBstBszn.Bszngxsj = DateTime.Now;//数据最后更新时间
                    mTbBstBszn.Sfsc = "0";// 0未删除1已删除 

                    mResultModel = TbBstBsznBLL.Update(mTbBstBszn, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                TbBstBszn mTbBstBszn = new TbBstBszn(); //声明实体
                TbBstDic mBaseDic = new TbBstDic();
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstBszn = TbBstBsznBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstBszn != null)
                    {
                        IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                        json = JsonConvert.SerializeObject(mTbBstBszn, dtConverter);
                        //解析是否热点
                        if (!String.IsNullOrEmpty(mTbBstBszn.Sfrd))
                        {
                            var list = TbBstBsznBLL.GetDicByCodeAndParentnode(mTbBstBszn.Sfrd, "bool");
                            mBaseDic = list.SingleOrDefault();
                            Hashtable hashtable = new Hashtable();
                            if (mBaseDic != null)
                            {
                                hashtable.Add("Bfrd", mBaseDic.Description);
                                json = JsonString.AddAttribute(json, hashtable);
                            }
                        }
                        //////解析分类名称
                        //if (!String.IsNullOrEmpty(mTbBstBszn.Cid.ToString()))
                        //{
                        //    var list = TbBstBsznBLL.GetDicByCodeAndParentnode(mTbBstBszn.Cid.ToString(), "bszn");
                        //    mBaseDic = list.SingleOrDefault();
                        //    Hashtable hashtable = new Hashtable();
                        //    if (mBaseDic != null)
                        //    {
                        //        hashtable.Add("VCid", mBaseDic.Description);
                        //        json = JsonString.AddAttribute(json, hashtable);
                        //    }
                        //}

                        //if (!String.IsNullOrEmpty(mTbBstBszn.Fwsj.ToString()))
                        //{
                        //    Hashtable hashtable = new Hashtable();
                        //    hashtable.Add("Fwsj", mTbBstBszn.Fwsj.ToString().Replace("T", " "));                     
                        //    json = JsonString.AddAttribute(json, hashtable);
                        //}
                        //if (!String.IsNullOrEmpty(mTbBstBszn.Jssj.ToString()))
                        //{
                        //    Hashtable hashtable = new Hashtable();
                        //    hashtable.Add("Jssj", mTbBstBszn.Jssj.ToString().Replace("T", " "));
                        //    json = JsonString.AddAttribute(json, hashtable);
                        //}
                        //if (!String.IsNullOrEmpty(mTbBstBszn.Kssj.ToString()))
                        //{
                        //    Hashtable hashtable = new Hashtable();
                        //    hashtable.Add("Kssj", mTbBstBszn.Kssj.ToString().Replace("T", " "));
                        //    json = JsonString.AddAttribute(json, hashtable);
                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string type = context.Request.Form["selType"];
            string name = context.Request.Form["txtName"];
            string bsjg = context.Request.Form["txtBSJG"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstBszn = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();

            SqlConditionList sqllist = new SqlConditionList();
            if (!string.IsNullOrEmpty(name))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstBsznConst.Columns.Swmc, '%' + name + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(bsjg))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstBsznConst.Columns.Bsjg, '%' + bsjg + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (type != "请选择" && !string.IsNullOrEmpty(type))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstBsznConst.Columns.Cid, type), SqlLogicOper.AND_LOGIC);
            }
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstBsznConst.Columns.Sfsc, "0"), SqlLogicOper.AND_LOGIC);   //0未删除  1已删除
            dtTbBstBszn = Q.GetRecordByPage("*", sqllist.ToSql(), "CID", intPageSize, intPageIndex, TbBstBsznConst.QTableName, TbBstBsznConst.FieldKey, "").Tables[0];

            if (dtTbBstBszn != null && dtTbBstBszn.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstBszn.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstBszn.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstBszn;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }


            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(tm, dtConverter);
            context.Response.Write(json);
        }
        #endregion

    }
}


